package com.bungarus.group.controller;

import com.bungarus.group.model.Group;
import com.bungarus.group.model.Member;
import com.bungarus.group.model.PagePayload;
import com.bungarus.group.model.SnowflakeIdMaker;
import com.bungarus.group.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by tang on 2018/12/12.
 */
@RestController
@RequestMapping("/api/gs/v1.0")
public class GroupController extends BungarusController{
    private GroupService groupService;
    private SnowflakeIdMaker idMaker = new SnowflakeIdMaker(15,16);

    @Autowired
    public GroupController(GroupService groupService) {
        this.groupService = groupService;
    }

    @GetMapping("/tenants/{tenantId}/groups/{groupId}/members")
    public ResponseEntity<PagePayload<Member>> doRetrieveMembersInGroup(
            @PathVariable("groupId")long gid, @PathVariable("tenantId")long tid,
            @RequestParam("offset")int offset,@RequestParam("size")int size) {

        if(gid < 0) {
            throw new IllegalArgumentException("group id must be greater than 0.");
        }
        Group group = new Group();
        group.set_id(gid);
        group.setTenantId(tid);

        List<Member> members = this.groupService.getMembers(group, offset, size);

        PagePayload<Member> pagePayload = new PagePayload<>();
        pagePayload.setPage(offset);
        pagePayload.setSize(size);
        pagePayload.setPageContents(members);
        pagePayload.setTotalCount(this.groupService.totalMemberCount(group));

        return ResponseEntity.ok()
                .cacheControl(CacheControl.noCache())
                .header("Pragma", "no-cache")
                .body(pagePayload);
    }

    @PostMapping(value = "/tenants/{tenantId}/groups/{groupId}/members",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public Member addMemberInGroup(@PathVariable("tenantId")long tid,
                                   @PathVariable("groupId")long gid,
                                   @RequestParam("id")long mid,
                                   @RequestParam("name")String mName,
                                   @RequestParam("account")String accountName) {
        Member member = Member.newBuilder()
                        .id(mid)
                        .name(mName)
                        .tenantId(tid)
                        .account(accountName)
                        .build();
        this.groupService.addMember(gid, member);
        return member;
    }

    @PostMapping(value = "/tenants/{tenantId}/groups", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    @ResponseStatus(HttpStatus.OK)
    public Group createGroup(@PathVariable("tenantId")long tid,
                             @RequestParam("id")long gid,
                             @RequestParam("name")String gname) {
        if(gid <= 0) {
            gid = idMaker.nextId();
        }
        Group group = Group.newBuilder().id(gid)
                        .tenant(tid)
                        .name(gname)
                        .build();
        return this.groupService.createGroup(group);
    }

    @RequestMapping(value = "/tenants/{tenantId}/groups/{groupId}",
            method = {RequestMethod.DELETE, RequestMethod.GET})
    @ResponseStatus(HttpStatus.OK)
    public void deleteGroup(@PathVariable("tenantId")long tid,
                            @PathVariable("groupId")long gid) {
        Group group = Group.newBuilder()
                .tenant(tid)
                .id(gid).build();
        this.groupService.deleteGroup(group);
    }
}
